package com.team.project.service.impl;

import com.team.project.dao.AdminDao;
import com.team.project.entity.Admin;
import com.team.project.service.intf.AdminService;
import com.team.project.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;

/**
 * @author 逗逗
 * 2022/11/22 17:41
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminDao adminDao;
    @Override
    public R login(String name, String password, String code, HttpSession session) {
        //1.验证参数
        if(StringUtils.hasLength(code) && StringUtils.hasLength(name)){

            //2.校验 验证码
            if(code.equalsIgnoreCase(session.getAttribute("code").toString())){

                //3.查询数据库
                Admin admin = adminDao.login(name);
                //4.验证是否存在
                if(admin!=null){
                    //账号存在

                    //5.验证密码
                    if(admin.getPassword().equals(password)){
                        //一样 登录成功
                        //6.记录在线的用户对象
                        session.setAttribute("admin",admin);
                        return R.ok();
                    }
                }
            }
        }
        return R.fail();
    }
}
